<example name="Auth">
  <file type="html">
    <div id="example">
    </div>
  </file>

  <file type="js">
    import Auth from '@jetbrains/ring-ui/components/auth/auth';
    import hubConfig from 'ring-ui-docs/components/hub-config';
    // Example config:
    // var hubConfig = {
    //   serverUri: 'https://hub.jetbrains.com/',
    //   clientId: '81a0bffb-6d0f-4a38-b93a-0a4d1e567698',
    //   requestCredentials: 'skip',
    //   redirectUri: window.location.href.split('#')[0]
    // };

    const log = function (title, obj) {
      const titleElem = document.createElement('h3');
      const jsonElem = document.createElement('div');

      titleElem.innerHTML = title;
      jsonElem.innerHTML = JSON.stringify(obj);

      document.getElementById('example').appendChild(titleElem);
      document.getElementById('example').appendChild(jsonElem);
    };

    (async auth => {
      try {
        const location = await auth.init();
        log('Location to restore:', location)
        const token = await auth.requestToken();
        log('Token:', token);
        const data = await auth.requestUser();
        log('User profile data:', data);
      } catch (e) {
        log('error', e);
      }
    })(new Auth(hubConfig));
  </file>
</example>


<example name="Auth landing page">
  <file type="html">
    <div id="example">
      <div id="example"></div>
      <div><a href="#" id="open-link" class="ring-link">Open landing page</a></div>
      <div><a href="#" id="force-update" class="ring-link">Force token update</a></div>
      <div><a href="#" id="log-out" class="ring-link">Log out</a></div>
    </div>
  </file>

  <file type="js">
    import Auth from '@jetbrains/ring-ui/components/auth/auth';
    import '@jetbrains/ring-ui/components/link/link.scss';
    import authService from '@jetbrains/ring-ui/components/auth-dialog-service/auth-dialog-service';
    import hubConfig from 'ring-ui-docs/components/hub-config';
    import LandingEntryFileName from '@jetbrains/ring-ui/components/auth/landing-entry';

    async function run() {
      const auth = new Auth({
        ...hubConfig,
        windowLogin: true,
        redirectUri: hubConfig.redirect_uri + LandingEntryFileName
      });

      auth.setAuthDialogService(authService);
      await auth.init();

      document.querySelector('#open-link').href = LandingEntryFileName;

      document.querySelector('#force-update').addEventListener('click', () => {
        auth.forceTokenUpdate();
      });

      document.querySelector('#log-out').addEventListener('click', () => {
        auth.login();
      });
    }

    run();

  </file>
</example>
